package com.web.server.common.utils;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZHANGXIANLIANG1 on 2016/3/8.
 */
public class ValidatorUtil {

    /**
     * 将校验结果转换成Map，用于返回前台
     */
    public static Map<String, String> getErrorMessages(BindingResult result) {
        Map<String, String> errors = new HashMap<String, String>();
        for (ObjectError error : result.getAllErrors()) {
            errors.put(((FieldError) error).getField(), error.getDefaultMessage());
        }
        return errors;
    }
}
